package linter.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DO.User;
import linter.POJO.DO.UserCard;
import linter.POJO.DTO.UserCardDTO;
import linter.custom.ResultVO;
import linter.service.UserCardService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static linter.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Api(tags = "用户卡片管理")
@RestController
@Slf4j
@RequestMapping("/userCard")
public class UserCardController {
    @Resource
    UserService userService;
    @Resource
    UserCardService userCardService;

    @PostMapping("/getUserNameAndImgHeadByToken")
    @ApiOperation("根据token获取用户名称和头像")
    public ResultVO<?> getUserNameAndImgHeadByToken(){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long id = userService.getIdByUsername(username);
        String userName = userCardService.getUserNameById(id);
        String imgUrlIcon = userCardService.getImgHeadById(id);
        Map dataMap = new HashMap<>();
        dataMap.put("userName",userName);
        dataMap.put("imgUrlIcon",imgUrlIcon);
        return ResultVO.success(dataMap);
    }

    @PostMapping("/updateUserCard")
    @ApiOperation("修改用户卡片")
    public ResultVO<?> updateUserCard(String nickname, Integer sex, String birthday, String signature, MultipartFile file){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            UserCardDTO userCardDTO = new UserCardDTO();
            userCardDTO.setNickname(nickname);
            userCardDTO.setSex(sex);
            long timestamp = Long.parseLong(birthday); // 将字符串转换为Long类型
            Date date = new Date(timestamp); // 使用时间戳创建Date对象
            userCardDTO.setBirthday(date);
            userCardDTO.setSignature(signature);
            return ResultVO.success(userCardService.updateUserCard(userCardDTO,uuid,file));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getUserCardByUuid")
    @ApiOperation("根据用户id获取用户卡片")
    public ResultVO<UserCard> getUserCardByUuid(){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(userCardService.getUserCardById(uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getUserCardByUuidForPosts")
    @ApiOperation("根据用户id获取用户卡片")
    public ResultVO<UserCard> getUserCardByUuidForPosts(Long Uuid){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(userCardService.getUserCardById(Uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getUser")
    @ApiOperation("根据用户id获取用户信息")
    public ResultVO<User> getUser(){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(userService.getById(uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }
}
